﻿using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using Controllers.Extend;
using Controllers.Filter;
using Model.Database;

namespace Controllers.WebApi
{
    //[EquipmentAuthorize]
    public class TestController : BaseApiController
    {
        /// <summary>
        /// 获取角色权限（异步）
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<VM_RolePower>> GetRolePower()
        {
            List<VM_RolePower> vmList = new List<VM_RolePower>();

            var task1 = GetPowerList();
            var task2 = GetRoleList();
            await Task.WhenAll(task1,task2);
            List<Power> powerList= task1.Result;
            List<Role> roleList = task2.Result;
            for (int i = 0; i < roleList.Count; i++)
            {
                VM_RolePower vm = new VM_RolePower();
                vm.Role = roleList[i];
                vm.Power = powerList;
                vmList.Add(vm);
            }

            return vmList;
        }

        /// <summary>
        /// 获取所有权限
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public List<Power> GetAllPower()
        {
            List<Power> powerList = CurrentBllSession.PowerBll.GetList();

            return powerList;
        }

        /// <summary>
        /// 获取所有角色
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public List<Role> GetAllRole()
        {
            List<Role> roleList = CurrentBllSession.RoleBll.GetList();

            return roleList;
        }

        /// <summary>
        /// 增加权限
        /// </summary>
        /// <param name="power"></param>
        /// <returns></returns>
        [HttpPost]
        public string AddPower(Power power)
        {
            return "ok";
        }

        /// <summary>
        /// 增加权限（Get方式）
        /// </summary>
        /// <param name="power"></param>
        /// <returns></returns>
        [HttpGet]
        public string AddPowerByGet([FromUri]Power power)
        {
            return "ok";
        }

        /// <summary>
        /// 获取字符串
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string GetString()
        {
            string str = "123";

            return str;
        }

        /// <summary>
        /// 获取数字
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public int GetInt()
        {
            int a = 6;

            return a;
        }

        /// <summary>
        /// 获取bool
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public bool GetBool()
        {
            bool a = true;

            return a;
        }
        
        /// <summary>
        /// webApi返回二进制流文件
        /// 如：http://***.***.**.com/a.jpg
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<HttpResponseMessage> GetJpg(string url)
        {
            HttpClient httpClient = new HttpClient();
            return httpClient.GetAsync(url);
        }









        #region 私有方法

        [NonAction]
        private async Task<List<Power>> GetPowerList()
        {
            var taskResult= await Task.Run(()=>
            {
                List<Power> powerList = CurrentBllSession.PowerBll.GetList_Async();

                return powerList;
            });

            return taskResult;
        }

        [NonAction]
        private async Task<List<Role>> GetRoleList()
        {
            var taskResult = await Task.Run(() =>
            {
                List<Role> roleList = CurrentBllSession.RoleBll.GetList_Async();

                return roleList;
            });

            return taskResult;
        }

        /// <summary>
        /// 角色权限
        /// </summary>
        public class VM_RolePower
        {
            /// <summary>
            /// 角色
            /// </summary>
            public Role Role { get; set; }
            /// <summary>
            /// 权限
            /// </summary>
            public List<Power> Power { get; set; }
           
        } 

        #endregion

    }
}
